package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
<<<<<<< HEAD
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
=======
import com.alibaba.fastjson.JSONObject;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
>>>>>>> adc48831cde55357ef0420c5deac2882fa85ad7a

import java.util.List;

/**
 * @ClassName SetmealMobileController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/27 10:02
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/setmeal")
public class SetmealMobileController {

    @Reference
    SetmealService setmealService;

<<<<<<< HEAD
    // 查询所有的套餐
    @RequestMapping(value = "/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> list = setmealService.findAll();
        if(list!=null && list.size()>0){
=======
    @Autowired
    JedisPool jedisPool;



    // 查询所有的套餐
    @RequestMapping(value = "/getSetmeal")
    public Result getSetmeal(){
        String setmeal = jedisPool.getResource().get(RedisConstant.SETMEAL_Setmeal_RESOURCES);
        if (setmeal != null){
            List<Setmeal> list= JSONObject.parseArray(setmeal,Setmeal.class);
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
        }
        List<Setmeal> list = setmealService.findAll();
        if(list!=null && list.size()>0){
            setmeal = JSONObject.toJSONString(list);
            jedisPool.getResource().setex(RedisConstant.SETMEAL_Setmeal_RESOURCES,5*60,setmeal);
>>>>>>> adc48831cde55357ef0420c5deac2882fa85ad7a
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
        }else{
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    // 使用套餐id，查询套餐详情
    @RequestMapping(value = "/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setmealService.findById(id);
        if(setmeal!=null){
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }else{
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
